package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
@Slf4j
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueMapper tbClueMapper;
    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    /**
     * 新增线索跟进记录,并修改线索表中相关数据
     * @param clueTrackRecordVo
     */
    @Override
    @Transactional
    public void addTbClueTrackRecord(ClueTrackRecordVo clueTrackRecordVo) {
        //创建一个线索跟进对象
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        //将请求参数中的线索跟进相关的数据复制到线索跟进对象中
        BeanUtils.copyBeanProp(tbClueTrackRecord,clueTrackRecordVo);
        //给线索跟进对象设置相应的,跟进人,当前跟进时间信息
        String username = SecurityUtils.getUsername();
        tbClueTrackRecord.setCreateBy(username);

        Date nowDate = DateUtils.getNowDate();
        tbClueTrackRecord.setCreateTime(nowDate);
        //将请求参数中的时间格式进行修改
        log.info("下次跟进时间为{}",clueTrackRecordVo.getNextTime());
        //调用mapper层的方法将数据存储到线索跟进表中
        tbClueTrackRecordMapper.addTbClueTrackRecord(tbClueTrackRecord);
        //创建一个线索对象
        TbClue tbClue = new TbClue();
        //将请求参数中和线索有关的数据复制到线索对象中
        BeanUtils.copyBeanProp(tbClue,clueTrackRecordVo);
        //根据clue_id设置线索id
        tbClue.setId(clueTrackRecordVo.getClueId());
        //给线索对象设置最新修改时间
        tbClue.setUpdateTime(DateUtils.getNowDate());
        //给线索对象设置状态为2:跟进中
        tbClue.setStatus("2");
        //调mapper层方法对线索表中的数据进行修改
        tbClueMapper.updateTbClue(tbClue);
    }

    /**
     * 根据线索id查询线索记录表,并分页返回所有的线索跟进信息
     * @param clueId
     * @return
     */
    public TableDataInfo findALLRecords(Long clueId){
        //创建一个分页数据对象
        TableDataInfo tableDataInfo = new TableDataInfo();
        //调用mapper层方法,获取查询到的数据集合
        List<TbClueTrackRecord> tbClueTrackRecords = tbClueTrackRecordMapper.findAllRecords(clueId);
        //获取list集合长度
        Long size = Long.valueOf(tbClueTrackRecords.size());
        tableDataInfo.setTotal(size);
        tableDataInfo.setRows(tbClueTrackRecords);
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        return  tableDataInfo;
    }
}
